package com.onenine.app.db;

import android.content.ContentValues;

import com.onenine.app.model.ChatGiftItem;
import com.onenine.app.model.CoterieItem;
import com.onenine.app.model.FriendIdItem;
import com.onenine.app.model.GameCategory;
import com.onenine.chat.model.LNewFriend;
import com.onenine.chat.model.LUser;

import org.litepal.crud.DataSupport;
import org.litepal.exceptions.DataSupportException;

import java.util.List;

/**
 * 创建时间：2018/7/24 0024 17:47
 * 创建人：fly
 * 功能描述：数据库工具管理
 */
public class DataManager {

    /**
     * 获取未回复的新朋友消息
     *
     * @return
     */
    public static List<LNewFriend> getAllFriendMessageByUserId(String userId) {
        return DataSupport.where("userId=?", userId).order("datetime desc").find(LNewFriend.class);
    }

    /**
     * 获取未读未回复的新朋友消息数量
     *
     * @return
     */
    public static int getAllFriendMessageCountByUserId(String userId) {
        return DataSupport.where("userId=?", userId).count(LNewFriend.class);
    }

    /**
     * 保存或更新打招呼或添加好友申请
     *
     * @param targetUserId
     * @param messageContent
     */
    public static void saveOrUpdateNewFriendMessageByUserId(String userId, String targetUserId, String messageContent) {
        LNewFriend newFriend = new LNewFriend();
        newFriend.setUserId(userId);
        newFriend.setTargetUserId(targetUserId);
        newFriend.setDatetime(System.currentTimeMillis());
        newFriend.setLastMessage(messageContent);
        newFriend.setRead(false);
        newFriend.setExtra("");
        newFriend.saveOrUpdate("userId=? and targetUserId =?"
                , userId, targetUserId);
    }

    /**
     * 更新消息已读
     *
     * @param userId
     * @param targetUserId
     */
    public static void updateNewFriendMessageReadedByUserId(String userId, String targetUserId) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("isRead", true);
        DataSupport.updateAll(LNewFriend.class, contentValues, "userId=? and targetUserId =?"
                , userId, targetUserId);
    }

    /**
     * 清空用户的所有新朋友消息
     *
     * @param userId
     */
    public static void deleteAllByUserId(String userId) {
        DataSupport.deleteAll(LNewFriend.class, "userId=?", userId);
    }

    /**
     * 保存所有好友id列表
     *
     * @param friendIdItemList
     */
    public static void saveAllFriendIds(List<FriendIdItem> friendIdItemList) {
        DataSupport.deleteAll(FriendIdItem.class);
        try {
            DataSupport.saveAll(friendIdItemList);
        } catch (DataSupportException e) {
            e.printStackTrace();
        }
    }


    /**
     * 保存所有聊天礼物列表
     *
     * @param chatGiftItemList
     */
    public static void saveAllChatGifts(List<ChatGiftItem> chatGiftItemList) {
        DataSupport.deleteAll(ChatGiftItem.class);
        try {
            DataSupport.saveAll(chatGiftItemList);
        } catch (DataSupportException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存所有游戏种类列表
     *
     * @param gameCategoryList
     */
    public static void saveAllGameCategorys(List<GameCategory> gameCategoryList) {
        DataSupport.deleteAll(GameCategory.class);
        try {
            DataSupport.saveAll(gameCategoryList);
        } catch (DataSupportException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有礼物
     *
     * @return
     */
    public static List<ChatGiftItem> getAllChatGifts() {
        return DataSupport.findAll(ChatGiftItem.class);
    }

    /**
     * 获取游戏种类
     *
     * @return
     */
    public static List<GameCategory> getAllGameCategorys() {
        return DataSupport.findAll(GameCategory.class);
    }

    /**
     * 根据目标用户id查询是否在我的好友id之列
     *
     * @param targetUserId
     * @return
     */
    public static boolean isMyFriend(String targetUserId) {
        List<FriendIdItem> friendIdItemList = DataSupport.where("userId=?", targetUserId).find(FriendIdItem.class);
        return friendIdItemList != null && !friendIdItemList.isEmpty();
    }

    /**
     * 保存所有圈子
     *
     * @param coterieItemList
     */
    public static void saveAllCoteries(List<CoterieItem> coterieItemList) {
        DataSupport.deleteAll(CoterieItem.class);
        try {
            DataSupport.saveAll(coterieItemList);
        } catch (DataSupportException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有圈子
     *
     * @return
     */
    public static List<CoterieItem> getAllCoteries() {
        return DataSupport.findAll(CoterieItem.class);
    }

    /**
     * 获取所有圈子但不包含指定类型
     *
     * @return
     */
    public static List<CoterieItem> getAllCoteriesExceptAsk(int exceptType) {
        return DataSupport.where("coterieType !=" + exceptType).find(CoterieItem.class);
    }

    /**
     * 删除用户缓存信息
     *
     * @param userId
     */
    public static void deleteUserByUserId(String userId) {
        DataSupport.deleteAll(LUser.class, "userId=?", userId);
    }

}
